home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / dev / e / rpmod1v1.lha / Modules / Tools / bsearch.doc < prev    next >
Encoding:
Text File  |  1995-04-06  |  1.5 KB  |  59 lines

  1.     Generalized BinarySearch function for anything ( bsearch.m )
  2.  
  3.     © Richard Perrott 6th April 1995 FreeWare
  4.       email: hcm94rp2@dmu.ac.uk
  5.  
  6. This module implements the binary sort algorithm in the most flexible way.
  7.  
  8.     PROC bsearch(base,first,last,find,comp) /* : LONG */
  9.  
  10. base:
  11.     the start address of an ARRAY OF ? e.g. CHAR, INT, LONG, OBJECT ?.
  12.  
  13. first:
  14.     the lowest position you want to search from
  15.  
  16. last:
  17.     the highest position you want to search to
  18.  
  19. find:
  20.     the item to find: value/pointer to value/etc
  21.  
  22. comp:
  23.     a pointer to a PROC to compare the value at position
  24.     with the value for find.
  25.  
  26.     e.g.
  27.  
  28.     PROC comp(base:PTR TO LONG, position, find:LONG)
  29.       DEF item
  30.  
  31.       item := base[position]
  32.       IF item1 < find THEN RETURN -1
  33.       IF item1 > find THEN RETURN 1
  34.     ENDPROC 0
  35.  
  36.  
  37. If search failsThe result is -1 ($FFFFFFFF) if search fails.
  38.  
  39. Note:
  40.   Any registers it uses are saved, but if your comp routine
  41. uses registers other than D0/D1/D2/A0 and the caller of bsearch
  42. needs them, then you will need to save them. bsearch doesn't
  43. care which registers you use.
  44.  
  45.   The module contains 96 bytes of assembler / E code.
  46.  
  47.   The routine will only stay in a 68020+ code cache if the
  48. PROC comp it calls is small and very close to it.
  49.  
  50.   The routine has been heavily tested, though there are no promises
  51. and I won't be held responsible if you find bugs or you misuse the
  52. routine.
  53.  
  54.   If you do find any bugs then please email me.
  55.  
  56. References:
  57.     Programming and Problem Solving in Modula-2, Sanford Leestma
  58.     & Larry Nyhoff, © Macmillan 1992, USA, page 380-381
  59.